//
//  HLBaseModel.m
//  s.
//
//  Created by fanshijian on 2022/8/24.
//  Copyright © 2022 J. All rights reserved.
//

#import "BaseModel.h"
#import "SpaceModel.h"

@implementation BaseModel

// 转字典时候，自动忽略某些字段
+ (NSArray *)mj_ignoredPropertyNames {
    return @[@"layout",@"idf",@"idfContent",@"idfBlock",@"action",@"kl_extra",@"modified"];
}

- (UHomeLayout *)layout{
    if (!_layout) _layout = [[UHomeLayout alloc] init];
    return _layout;
}

- (void)prepareLayoutDefault{}

- (void)prepareLayout:(CGSize)size{
    self.layout.itemSize = size;
}

- (instancetype)init {
    if (self = [super init]) {
        _modified = NO;
    }
    return self;
}

+ (instancetype)modelWithSize:(CGSize)size idf:(NSString *)idf {
    BaseModel *model = [[self class] new];
    if (idf) model.idfBlock = ^NSString *{return idf;};
    if (size.width > 0 && size.height > 0) [model prepareLayout:size];
    return model;
}

- (NSString *)idf{
    if (self.idfBlock) return self.idfBlock();
    NSAssert(0, @"未知类型cell标识符");
    return nil;
}

- (NSString *)idfContent {
    return nil;
}

SpaceModel *space(CGFloat h){
    return space_s(kScreenWidth, h);
}
SpaceModel *space_s(CGFloat w, CGFloat h){
    return space_sc(kSize(w, h), nil);
}
SpaceModel *space_ff(CGFloat h){
    return space_s_ff(kScreenWidth, h);
}
SpaceModel *space_f5(CGFloat h){
    return space_s_f5(kScreenWidth, h);
}
SpaceModel *space_s_ff(CGFloat w, CGFloat h){
    return space_sc(kSize(w, h), @"#FFFFFF");
}
SpaceModel *space_s_f5(CGFloat w, CGFloat h){
    return space_sc(kSize(w, h), @"#F5F5F5");
}
SpaceModel *space_sc(CGSize size, NSString * color){
    return [SpaceModel spaceWithColor:color size:size];
}
SpaceModel *t_space_hc(CGFloat h, NSString * __nullable color){
    SpaceModel *sm = [SpaceModel spaceWithColor:color size:kSize(kScreenWidth, h)];
    sm.idfBlock = ^NSString * _Nonnull{
        return @"SpacingTableCell";
    };
    return sm;
}

#pragma mark - CHGCollectionViewCellModelProtocol
- (NSString *)cellClassNameInCollectionView:(UICollectionView*)collectionView atIndexPath:(NSIndexPath*)indexPath {
    return self.idf;
}

- (CGSize)chg_collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return self.layout.itemSize;
}

#pragma mark - CHGTableViewCellModelProtocol
/**
 绑定一个cell 类
 
 @return 返回类名
 */
-(NSString*)cellClassNameInTableView:(UITableView*)tableView indexPath:(NSIndexPath*)indexPath {
    return self.idf;
}

/**
 返回当前cell的高度
 
 @return cell、headerFooter的高度
 */
-(CGFloat)cellHeighInTableView:(UITableView*)tableView indexPath:(NSIndexPath*)indexPath {
    return self.layout.itemSize.height;
}

-(BOOL)cellCanEditInTableView:(UITableView*)tableView indexPath:(NSIndexPath*)indexPath {
    return self.canEdit;
}
-(UITableViewCellEditingStyle)editingStyleInTableView:(UITableView*)tableView indexPath:(NSIndexPath*)indexPath {
    return  UITableViewCellEditingStyleDelete;
}
-(NSString*)titleForDeleteConfirmationButtonInTableView:(UITableView*)tableView indexPath:(NSIndexPath*)indexPath {
    return SWString(@"删除");
}

-(NSArray<UITableViewRowAction*>*) editActionsInTableView:(UITableView*)tableView indexPath:(NSIndexPath*)indexPath {
    if (self.editAction.count == 0) {
        UITableViewRowAction *deleteRoWAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:SWString(@"删除") handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
            
            if (self.editActionBlock) {
                self.editActionBlock(action, indexPath);
            }
            
           }];
           return @[deleteRoWAction];
    }else {

           return self.editAction;
    }
  
}

@end
